package com.mondo.financialwallet.presentation.ui.activity;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.text.Html;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.common.ui.BaseActivity;
import com.example.common.utils.TimeCount;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.params.SendMessageParams;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;
import com.mondo.financialwallet.presentation.utils.LocalConfigStorage;
import com.mondo.financialwallet.presentation.utils.Toasts;

import butterknife.Bind;

/**
 * 修改登录密码
 */
public class AlterLoginPasswordActivity extends BaseActivity {

    @Bind(R.id.hint_text_view) TextView mHintTextView;
    @Bind(R.id.old_password_edit) EditText mOldPasswordEdit;
    @Bind(R.id.new_password_edit) EditText mNewPasswordEdit;
    @Bind(R.id.auth_code_edit) EditText mAuthCodeEdit;
    @Bind(R.id.send_verification_code) TextView mSendVerificationCode;
    @Bind(R.id.sure_button) Button mSureButton;

    private ProgressDialog mProgressDialog;

    @Override
    protected int getBackgroundColor() {
        return Color.WHITE;
    }

    @Override
    protected void setStatusBar() {
        ActivityTitleBarHelper.setWhiteDarkColorStatus(this);
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_alter_login_password;
    }

    @Override
    protected void initActions() {
        ActivityTitleBarHelper.init(this, R.string.title_alter_password);

        // 提示
        mHintTextView.setText(Html.fromHtml(String.format(
                getString(R.string.text_get_verification_code_hint),
                "+" + LocalConfigStorage.get().getPrefix()
                        + " "
                        + LocalConfigStorage.get().getMobile()
                ))
        );

        // 发送验证码
        mSendVerificationCode.setOnClickListener(v -> requestMessageCode());

        // 修改密码
        mSureButton.setOnClickListener(v -> requestAlterPassword());
    }

    private void requestAlterPassword() {
        String oldPassword = mOldPasswordEdit.getText().toString();
        String newPassword = mNewPasswordEdit.getText().toString();
        String code = mAuthCodeEdit.getText().toString();

        DataRepository.get().alterLoginPassword(oldPassword, newPassword, code, result -> {
            if (result.isSuccess()) {
                Toasts.success(R.string.message_alter_success);
                finish();
                LocalConfigStorage.get().setPassword(newPassword);
            } else {
                Toasts.failure(result.getMsg());
            }
        });
    }

    private void requestMessageCode() {
        String mobile = LocalConfigStorage.get().getMobile();
        String prefix = LocalConfigStorage.get().getPrefix();

        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog.show(this, null,
                    getString(R.string.message_sending_sms_verification_code));

            DataRepository.get().sendMessageCode(
                    new SendMessageParams(mobile, prefix),
                    result -> {
                        mProgressDialog.dismiss();
                        mProgressDialog = null;

                        if (result.isSuccess()) {
                            new TimeCount(mSendVerificationCode).start();
                        } else {
                            Toasts.failure(result.getMsg());
                        }
                    }
            );
        }
    }
}
